package com.qing.demo.netty.demo1.nio.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

/**
 * @author caozhipeng
 * @date 2025/7/22
 */
public class NioClient {
    public static void main(String[] args) {
        try {
            Selector selector=Selector.open();
            SocketChannel socketChannel=SocketChannel.open();
            socketChannel.configureBlocking(false);

            boolean isConnect=socketChannel.connect(new InetSocketAddress("127.0.0.1",7379));
            if(!isConnect){
                socketChannel.register(selector, SelectionKey.OP_CONNECT);
            }else {
                socketChannel.register(selector, SelectionKey.OP_READ);
            }
            System.out.println("客户端启动成功");
            new NioClientHandler(selector, Charset.forName("utf-8")).start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
